package com.sohu.peak.gallery.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.sohu.peak.gallery.dao.OrignDAO;
import com.sohu.peak.gallery.domain.Orign;
import com.sohu.peak.util.HibernateHelper;
import com.sohu.peak.util.Logger;
import com.sohu.peak.util.Pagination;

public class OrignHibernateDAO extends BaseHibernateDAO implements OrignDAO {
	private static final Logger log = new Logger();
	public static final String OR_NAME = "name";
	@SuppressWarnings("unchecked")
	public List<Orign> findAll() {
		log.debug("finding all Orign instances");
		try {
			String queryString = "from Orign";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			return null;
		}
	}
	public List<Orign> find(Pagination pagination) {
		log.debug("finding pagination Orign instances");
		try {
			String queryString = "from Orign";
			return new ArrayList<Orign>(HibernateHelper.pageQuery(getSession(), getSession().createQuery(queryString), pagination, null));
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			return null;
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Orign> findAll(int status) {
		log.debug("finding all Orign instances");
		try {
			String queryString = "from Orign where status=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, status);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			return null;
		}
	}
	
	public Orign save(Orign orign) {
		
		List<Orign> list=this.findByName(orign.getName());
		if(list.size()>0){
			orign.setId(-2L);
			return orign;
		}
		
		log.debug("saving PicGroup instance");
		try {
			getSession().save(orign);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
		}
		return orign;
	}
	
	public List<Orign> findByName(Object name) {
		return findByProperty(OR_NAME, name);
	}

	public Orign findById(java.lang.Long id) {
		log.debug("getting Orign instance with id: " + id);
		try {
			Orign instance = (Orign) getSession().get(
					"com.sohu.peak.gallery.domain.Orign", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return null;
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Orign instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Orign as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			return new ArrayList(0);
		}
	}
	
	public Boolean update(Orign orign) {
		log.debug("attaching dirty orign instance");
		try {
			getSession().update(orign);
			log.debug("orign successful");
			return Boolean.TRUE;
		} catch (RuntimeException re) {
			log.error("orign failed", re);
			return Boolean.FALSE;
		}
	}
	

	public Boolean del(Orign persistentInstance) {
		log.debug("deleting Orign instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
			return Boolean.TRUE;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			return Boolean.FALSE;
		}
	}
	public int batchAdd(List<Orign> list) {
		int count =0;
		for (Orign orign : list) {
			if(orign!=null){
				count++;
				orign.setId(null);
				this.save(orign);
			}
		}
		return 0;
	}
	public int batchDelete(List<Orign> list) {
		int count=0;
		for (Orign orign : list) {
			if(orign!=null && orign.getId()  > 0){
				count++;
				this.del(orign);
			}
		}
		return count;
	}
	public int batchUpdate(List<Orign> list) {
		int count=0;
		for (Orign orign : list) {
			if(orign!=null && orign.getId()>0){
				count++;
				this.update(orign);
			}
		}
		return count;
	}
}
